<?php

require_once("libs/controller.php");
require_once 'modelos/articulo.php';
require_once 'modelos/fotos_articulo.php';
require_once 'modelos/cotizacion.php';
require_once 'modelos/comentarios.php';

/**
 * Class with all function for a seller.
 * Every xMethod allways receives two aditional parameters:
 * - id        : User id who makes the request
 * - lang      : Lang of user device
 * - user_type : User type (V / C / G)
 *
 * @author Gonfre
 */
class SellerController extends Controller {
	
	public function __construct() {
	}
	
	
	public function index() {
	}

	
	/**
	 * Returns a product request list for a seller
	 * @param categories
	 * @param locations
	 * @param with_photo
	 */	
	public function xGetRequestList() {
		extract($_REQUEST);
		
		$a = new Articulo();
		if ($a->getProductRequestsBySeller($id, $categories, $locations, $with_photo, $lang)) {
			
			$response = $this->createResponseHeader(0, $lang);
			
			$list = array();
			while ($a->next()) {
				array_push($list, array(
					"id" => $a->getValue("id"),
					"description" => $a->getValue("descripcion"),
					//"category" => $a->getValue("nombre"),
					"category" => $a->getValue("id_categoria"),
					"with_photo" => $a->getValue("con_foto"),
					"date" => $a->getValue("fecha"),
					"location" => $a->getValue("id_ubicacion"),
					//"location" => $a->getValue("ubicacion")
					"purchaser" => $a->getValue("comprador")
				));				
			}
			
			if (count($list) == 0) {
				$this->returnKO(2, $lang);
			}
			
			$response = $response + array (
					"body" => array (
							"request_list" => $list
					)
			);
			
			$this->putJsonResponse($response);
			
		} else {
			$this->returnKO(-8, $lang);
		} 
		
	}
	
	
	/**
	 * Sends a price for a requested product
	 * @param id_request
	 * @param id_purchaser
	 * @param price
	 * @param comment
	 */
	public function xSendPrice() {
		extract($_REQUEST);
		
		$c = new Cotizacion();
		$c->getConnection()->iniciarTransaccion();
		
		$c->setValue("id_articulo", $id_request);
		$c->setValue("precio", $price);
		$c->setValue("id_vendedor", $id);
		$c->setValue("id_comprador", $id_purchaser);
		
		if ($c->doSave()) {
			$price_id = $c->getLastSavedId();
			
			//Insertar comentario
			$comment_id = -1;
			if ($comment != "") {
				$com = new Comentarios();
				$com->setValue("id_cotizacion", $price_id);
				$com->setValue("texto", $comment);
				$com->setValue("origen", "V");
				
				if ($com->doSave()) {
					$comment_id = $com->getLastSavedId();
				} else {
					$this->returnKO(-8, $lang, $c->getConnection());
				}
			}
			
			//preparo la respuesta final
			$response = $this->createResponseHeader(0, $lang);
			
			$response = $response + array (
					"body" => array (
							"id_price" => $price_id,
							"id_comment" => $comment_id
					)
			);
				
			$c->getConnection()->confirmarTransaccion();
			$this->putJsonResponse($response);
		} else {
			$this->returnKO(-8, $lang);
		}
	}
	
	
	/**
	 * Returns details for a product request
	 * @param id_request
	 */
	public function xGetRequestDetails() {
		extract($_REQUEST);
		
		$a = new Articulo($id);
		if ($a->doSelectAllWithForeign("usuario", "id_comprador") && $a->next()) {
			
			$response = $this->createResponseHeader(0, $lang);
			
			//get photos
			$f = new Fotos_articulo();
			$photo_list = array();
			if ($f->doSelectAllWithCondition("id_articulo", $id_request)) {
				while ($f->next()) {
					array_push($photo_list, array(
						"id_photo" => $f->getValue("id_foto")
					));
				}
			}

			//build response
			$response = $response + array (
					"body" => array (
							"description" => $a->getValue("descripcion"),
							"category" => $a->getValue("id_categoria"),
							"with_photo" => $a->getValue("con_foto"),
							"date" => $a->getValue("fecha"),
							"location" => $a->getValue("id_ubicacion"),
							"id_purchaser" => $a->getValue("id_comprador"),
							"purchaser" => $a->getValue("nombre"),
							"photo_count" => count($photo_list),
							"photo_list" => $photo_list
					)
			);
			
			$this->putJsonResponse($response);
			
		} else {
			$this->returnKO(2, $lang);
		}
	}
}

?>